﻿using System;
using System.Xml.Serialization;
using System.ComponentModel;
using ICeFramework.Utilities.Text;
using SharpGen.Enums;

namespace SharpGen.Templates
{
    [Serializable]
    public class StringReplacement : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _prefix;
        [XmlElement]
        public string Prefix
        {
            get { return _prefix; }
            set
            {
                string oldVal = _prefix;
                _prefix = value;
                if (oldVal != value)
                {
                    OnPropertyChanged("Prefix");
                }
            }
        }

        private string _suffix;
        [XmlElement]
        public string Suffix
        {
            get { return _suffix; }
            set
            {
                string oldVal = _suffix;
                _suffix = value;
                if (oldVal != value)
                {
                    OnPropertyChanged("Suffix");
                }
            }
        }

        private string _find;
        public string Find
        {
            get { return _find; }
            set 
            {
                string oldVal = _find;
                _find = value;
                if (oldVal != value)
                {
                    OnPropertyChanged("Find");
                }
            }
        }

        private string _replace;
        public string Replace
        {
            get { return _replace; }
            set 
            {
                string oldVal = _replace;
                _replace = value;
                if (oldVal != value)
                {
                    OnPropertyChanged("Replace");
                }
            }
        }

        private FindOptions _findOptions;
        [XmlAttribute]
        public FindOptions FindOptions
        {
            get { return _findOptions; }
            set 
            {
                FindOptions oldVal = _findOptions;
                _findOptions = value;
                if (!oldVal.Equals(value))
                {
                    OnPropertyChanged("FindOptions");
                }
            }
        }

        private StringReplacementType _replacementType;
        [XmlAttribute]
        public StringReplacementType ReplacementType
        {
            get { return _replacementType; }
            set
            {
                StringReplacementType oldVal = _replacementType;
                _replacementType = value;
                if (oldVal != value)
                {
                    OnPropertyChanged("ReplacementType");
                }
            }
        }

        private SqlObjectType _objectType;
        [XmlAttribute]
        public SqlObjectType ObjectType
        {
            get { return _objectType; }
            set
            {
                SqlObjectType oldVal = _objectType;
                _objectType = value;
                if (oldVal != value)
                {
                    OnPropertyChanged("ObjectType");
                }
            }
        }

        public StringReplacement()
        {

        }

        public string ReplaceText(string text, StringReplacementType replaceType, SqlObjectType objectType)
        {
            if ((_replacementType != replaceType && _replacementType != StringReplacementType.All && replaceType != StringReplacementType.All) ||
                (_objectType != objectType && _objectType != SqlObjectType.NA && objectType != SqlObjectType.NA))
            {
                return text;
            }
            string val = text;
            val = Strings.FindAndReplace(text, _find, _replace, _findOptions);
            val = string.Format("{0}{1}{2}", _prefix, val, _suffix);

            return val;
        }

        protected void OnPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}
